package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.MyFragmentPageAdapter;
import com.ly.lypopupwindowdemo.fragment.Fragment1;
import com.ly.lypopupwindowdemo.fragment.Fragment2;
import com.ly.lypopupwindowdemo.fragment.Fragment3;
import com.ly.lypopupwindowdemo.fragment.Fragment4;
import com.ly.lypopupwindowdemo.fragment.Fragment5;
import com.ly.lypopupwindowdemo.pagerTrans.DepthPageTransformer;

import java.util.ArrayList;
import java.util.List;

public class XuQiuActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    //定义Fragment
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;
    private Fragment fragment5;
    //定义FragmentManager
    private FragmentManager fragmentManager;

    //定义组件
    private ViewPager viewPager;
    private List<Fragment> fragmentLists;
    private MyFragmentPageAdapter adapter;
    private RadioGroup radioGroup;
    private RadioButton home; // 表示第一个RadioButton 组件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xu_qiu);

        //初始化界面组件
        init();
        //初始化ViewPager
        initViewPager();
    }

    private void initViewPager() {
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fragment5 = new Fragment5();

        fragmentLists = new ArrayList<Fragment>();
        fragmentLists.add(fragment1);
        fragmentLists.add(fragment2);
        fragmentLists.add(fragment3);
        fragmentLists.add(fragment4);
        fragmentLists.add(fragment5);
        //获取FragmentManager对象
        fragmentManager = getSupportFragmentManager();
        //获取FragmentPageAdapter对象
        adapter = new MyFragmentPageAdapter(fragmentManager, fragmentLists);
        //设置Adapter，使ViewPager 与 Adapter 进行绑定
        viewPager.setAdapter(adapter);
        //设置ViewPager默认显示第一个View
        viewPager.setCurrentItem(0);
        //设置第一个RadioButton为默认选中状态
        home.setChecked(true);
        //ViewPager页面切换监听
        viewPager.addOnPageChangeListener(this);

        viewPager.setPageTransformer(true,new DepthPageTransformer());

    }

    private void init() {
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        home = (RadioButton) findViewById(R.id.rb1);
        //RadioGroup状态改变监听
        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                radioGroup.check(R.id.rb1);
//                RadioButton rb1 = (RadioButton) radioGroup.findViewById(R.id.rb1);
//                rb1.setChecked(true);
                break;
            case 1:
                radioGroup.check(R.id.rb2);
//                RadioButton rb2 = (RadioButton) radioGroup.findViewById(R.id.rb1);
//                rb2.setChecked(true);
                break;
            case 2:
                radioGroup.check(R.id.rb3);
//                RadioButton rb3 = (RadioButton) radioGroup.findViewById(R.id.rb1);
//                rb3.setChecked(true);
                break;
            case 3:
                radioGroup.check(R.id.rb4);
//                RadioButton rb4 = (RadioButton) radioGroup.findViewById(R.id.rb1);
//                rb4.setChecked(true);
                break;
            case 4:
               radioGroup.check(R.id.rb5);
//                RadioButton rb5 = (RadioButton) radioGroup.findViewById(R.id.rb1);
//                rb5.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.rb1:
                //显示第一个Fragment并关闭动画效果
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.rb2:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.rb3:
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.rb4:
                viewPager.setCurrentItem(3, false);
                break;
            case R.id.rb5:
                viewPager.setCurrentItem(4, false);
                break;
        }
    }
}
